#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class VoteProvider : CY.GFive.Core.Providers.DALProvider.IVoteProvider
	{
		#region Sql
		private static readonly string SqlSelectVote = "SELECT [Id], [VoteTitle],[VoteStartTime],[VoteLimit],[VoteSate] FROM [Vote] WHERE  [Id]=@Id";

		private static readonly string SqlInsertVote = "INSERT INTO [Vote]([VoteTitle],[VoteStartTime],[VoteLimit],[VoteSate]) values(@VoteTitle,@VoteStartTime,@VoteLimit,@VoteSate);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateVote = "UPDATE [Vote] SET [VoteTitle] = @VoteTitle,[VoteStartTime] = @VoteStartTime,[VoteLimit] = @VoteLimit,[VoteSate] = @VoteSate WHERE  [Id]=@Id";

		private static readonly string SqlDeleteVote = "DELETE [Vote] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllVote = "SELECT [Id], [VoteTitle],[VoteStartTime],[VoteLimit],[VoteSate] FROM [Vote]";
		#endregion

		#region IVoteProvider Members
		public Core.Business.Vote Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectVote);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.Vote vote = new Core.Business.Vote();

				if (!reader.IsDBNull(0)) vote.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) vote.VoteTitle = reader.GetString(1);
				if (!reader.IsDBNull(2)) vote.VoteStartTime = reader.GetDateTime(2);
				if (!reader.IsDBNull(3)) vote.VoteLimit = reader.GetInt32(3);
				if (!reader.IsDBNull(4)) vote.VoteSate = reader.GetInt32(4);

				return vote;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.Vote vote)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@VoteTitle", SqlDbType.NVarChar, vote.VoteTitle);
			sql.AddParameter("@VoteStartTime", SqlDbType.DateTime, vote.VoteStartTime);
			sql.AddParameter("@VoteLimit", SqlDbType.Int, vote.VoteLimit);
			sql.AddParameter("@VoteSate", SqlDbType.Int, vote.VoteSate);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertVote);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) vote.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.Vote vote)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@VoteTitle", SqlDbType.NVarChar, vote.VoteTitle);
			sql.AddParameter("@VoteStartTime", SqlDbType.DateTime, vote.VoteStartTime);
			sql.AddParameter("@VoteLimit", SqlDbType.Int, vote.VoteLimit);
			sql.AddParameter("@VoteSate", SqlDbType.Int, vote.VoteSate);
			sql.AddParameter("@Id", SqlDbType.Int, vote.Id);
			sql.ExecuteSql(SqlUpdateVote);
		}

		public void Delete(Core.Business.Vote vote)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, vote.Id);
			sql.ExecuteSql(SqlDeleteVote);
		}

		public IList<Core.Business.Vote> GetAllVote()
		{
			IList<Core.Business.Vote> votelist = new List<Core.Business.Vote>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllVote);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.Vote vote = new Core.Business.Vote();

					if (!reader.IsDBNull(0)) vote.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) vote.VoteTitle = reader.GetString(1);
					if (!reader.IsDBNull(2)) vote.VoteStartTime = reader.GetDateTime(2);
					if (!reader.IsDBNull(3)) vote.VoteLimit = reader.GetInt32(3);
					if (!reader.IsDBNull(4)) vote.VoteSate = reader.GetInt32(4);

					vote.MarkOld();
					votelist.Add(vote);
				}
				reader.Close();
			}
			return votelist;
		}
		#endregion
	}
}
